package com.qding.park.manager.timer;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import com.qding.park.manager.timer.conf.TaskConfig;
import com.qding.park.manager.timer.schedule.task.TaskInfo;



@Component("timerContext")
public class TimerContext {

	private static final Log _log = LogFactory.getLog(TimerContext.class);
	
	public static final String TASKID_KEY="TASKID_KEY";
	
	public static final String taskGroupName="group1";

	
	/**
	 * 锁车超时检查任务名称
	 */
	public static final String  CarLockCheckJob_NAME="CarLockCheckJob";
	private Scheduler sched;

	public TimerContext() throws SchedulerException {
		SchedulerFactory sf = new StdSchedulerFactory();
		sched = sf.getScheduler();
	}
	
	
//	@Resource(name = "taskList")
//	private List<TaskInfo> taskList;
	
	private List<TaskInfo>  getTaskList(){
		
		List<TaskInfo> taskList=new ArrayList<TaskInfo>();
		String task_CarLockCheckJob_name=TaskConfig.getTask_CarLockCheckJob_name().trim();
		String task_CarLockCheckJob_taskJobClass= TaskConfig.getTask_CarLockCheckJob_taskJobClass().trim();
		String task_CarLockCheckJob_cronTime=TaskConfig.getTask_CarLockCheckJob_cronTime().trim();
		if(StringUtils.isBlank(task_CarLockCheckJob_name)||
				StringUtils.isBlank(task_CarLockCheckJob_taskJobClass)||
				StringUtils.isBlank(task_CarLockCheckJob_cronTime)){
			_log.info("task_CarLockCheckJob_name:"+"没有配置信息不启动");
		}else{
			TaskInfo  taskInfo=new TaskInfo();
			taskInfo.setName(task_CarLockCheckJob_name);
			taskInfo.setCronTime(task_CarLockCheckJob_cronTime);
			taskInfo.setTaskJobClass(task_CarLockCheckJob_taskJobClass);
			taskList.add(taskInfo);
		}
		
		
		String task_clearTimeOutOrderJob_cronTime=TaskConfig.getTask_clearTimeOutOrderJob_cronTime();
		String task_clearTimeOutOrderJob_name=TaskConfig.getTask_clearTimeOutOrderJob_name();
		String task_clearTimeOutOrderJob_taskJobClass=TaskConfig.getTask_clearTimeOutOrderJob_taskJobClass();
		if(StringUtils.isBlank(task_clearTimeOutOrderJob_cronTime)||
				StringUtils.isBlank(task_clearTimeOutOrderJob_name)||
				StringUtils.isBlank(task_clearTimeOutOrderJob_taskJobClass)){
			_log.info("task_clearTimeOutOrderJob_name:"+"没有配置信息不启动");
		}else{
			TaskInfo  taskInfo=new TaskInfo();
			taskInfo.setName(task_clearTimeOutOrderJob_name);
			taskInfo.setCronTime(task_clearTimeOutOrderJob_cronTime);
			taskInfo.setTaskJobClass(task_clearTimeOutOrderJob_taskJobClass);
			taskList.add(taskInfo);
		}
		
		
		String task_FailTaskRedoJob_cronTime= TaskConfig.getTask_FailTaskRedoJob_cronTime();
		String task_FailTaskRedoJob_name= TaskConfig.getTask_FailTaskRedoJob_name();
		String task_FailTaskRedoJob_taskJobClass =TaskConfig.getTask_FailTaskRedoJob_taskJobClass();
		if(StringUtils.isBlank(task_FailTaskRedoJob_cronTime)||
				StringUtils.isBlank(task_FailTaskRedoJob_name)||
				StringUtils.isBlank(task_FailTaskRedoJob_taskJobClass)){
			_log.info("task_FailTaskRedoJob_name:"+"没有配置信息不启动");
		}else{
			TaskInfo  taskInfo=new TaskInfo();
			taskInfo.setCronTime(task_FailTaskRedoJob_cronTime);
			taskInfo.setName(task_FailTaskRedoJob_name);
			taskInfo.setTaskJobClass(task_FailTaskRedoJob_taskJobClass);
			taskList.add(taskInfo);
		}
		
		String task_InviteCheckJob_cronTime=TaskConfig.getTask_InviteCheckJob_cronTime();
		String task_InviteCheckJob_name=TaskConfig.getTask_InviteCheckJob_name();
		String task_InviteCheckJob_taskJobClass=TaskConfig.getTask_InviteCheckJob_taskJobClass();
		if(StringUtils.isBlank(task_InviteCheckJob_cronTime)||
				StringUtils.isBlank(task_InviteCheckJob_name)||
				StringUtils.isBlank(task_InviteCheckJob_taskJobClass)){
			_log.info("task_InviteCheckJob_name:"+"没有配置信息不启动");
		}else{
			TaskInfo  taskInfo=new TaskInfo();
			taskInfo.setCronTime(task_InviteCheckJob_cronTime);
			taskInfo.setName(task_InviteCheckJob_name);
			taskInfo.setTaskJobClass(task_InviteCheckJob_taskJobClass);
			taskList.add(taskInfo);
		}
		
		
		String task_ParkBaseDataSync_name= TaskConfig.getTask_ParkBaseDataSync_name();
		String task_ParkBaseDataSyncJob_cronTime= TaskConfig.getTask_ParkBaseDataSyncJob_cronTime();
		String task_ParkBaseDataSyncJob_taskJobClass=TaskConfig.getTask_ParkBaseDataSyncJob_taskJobClass();
		if(StringUtils.isBlank(task_ParkBaseDataSync_name)||
				StringUtils.isBlank(task_ParkBaseDataSyncJob_cronTime)||
				StringUtils.isBlank(task_ParkBaseDataSyncJob_taskJobClass)){
			_log.info("task_InviteCheckJob_name:"+"没有配置信息不启动");
		}else{
			TaskInfo  taskInfo=new TaskInfo();
			taskInfo.setCronTime(task_ParkBaseDataSyncJob_cronTime);
			taskInfo.setName(task_ParkBaseDataSync_name);
			taskInfo.setTaskJobClass(task_ParkBaseDataSyncJob_taskJobClass);
			taskList.add(taskInfo);
		}
		
		String task_ReserveCheckJob_cronTime=TaskConfig.getTask_ReserveCheckJob_cronTime();
		String task_ReserveCheckJob_name=TaskConfig.getTask_ReserveCheckJob_name();
		String task_ReserveCheckJob_taskJobClass=TaskConfig.getTask_ReserveCheckJob_taskJobClass();
		if(StringUtils.isBlank(task_ReserveCheckJob_name)||
				StringUtils.isBlank(task_ReserveCheckJob_cronTime)||
				StringUtils.isBlank(task_ReserveCheckJob_taskJobClass)){
			_log.info("task_ReserveCheckJob_name:"+"没有配置信息不启动");
		}else{
			TaskInfo  taskInfo=new TaskInfo();
			taskInfo.setCronTime(task_ReserveCheckJob_cronTime);
			taskInfo.setName(task_ReserveCheckJob_name);
			taskInfo.setTaskJobClass(task_ReserveCheckJob_taskJobClass);
			taskList.add(taskInfo);
		}
		
		String task_ExpireNoticeJob_cronTime=TaskConfig.getTask_ExpireNoticeJob_cronTime();
		String task_ExpireNoticeJob_name=TaskConfig.getTask_ExpireNoticeJob_name();
		String task_ExpireNoticeJob_taskJobClass=TaskConfig.getTask_ExpireNoticeJob_taskJobClass();
		if(StringUtils.isBlank(task_ExpireNoticeJob_name)||
				StringUtils.isBlank(task_ExpireNoticeJob_cronTime)||
				StringUtils.isBlank(task_ExpireNoticeJob_taskJobClass)){
			_log.info("task_ExpireNoticeJob_name:"+"没有配置信息不启动");
		}else{
			TaskInfo  taskInfo=new TaskInfo();
			taskInfo.setCronTime(task_ExpireNoticeJob_cronTime);
			taskInfo.setName(task_ExpireNoticeJob_name);
			taskInfo.setTaskJobClass(task_ExpireNoticeJob_taskJobClass);
			taskList.add(taskInfo);
		}
		return taskList;
	}
	
	
	
	public void startTask() throws ClassNotFoundException, SchedulerException {

		_log.info("启动TimerContext");
		List<TaskInfo> taskList=getTaskList();
		for (TaskInfo task : taskList) {
			_log.info(task);
			String taskId=task.getId();
			String taskClassName=task.getTaskJobClass();
			String taskName=task.getName();
			String cronTime=task.getCronTime();
			Class<? extends Job> taskClass=(Class<? extends Job>) Class.forName(taskClassName.trim());
			JobDetail job = newJob(taskClass).withIdentity(taskName, taskGroupName).build();
			job.getJobDataMap().put(TASKID_KEY, taskId);
			CronTrigger trigger = newTrigger().withIdentity(taskName, taskGroupName).withSchedule(cronSchedule(cronTime))
			        .build();
			
			Date ft = sched.scheduleJob(job, trigger);
			_log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
		             + trigger.getCronExpression());
		}
		
		
		 sched.start();
		 
		 _log.info("------- Started Scheduler -----------------");

	}

	public Scheduler getSched() {
		return sched;
	}
	
	
}
